﻿package  com.pool.core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import com.pool.Config;
	import flash.display.BlendMode;
	import flash.display.GradientType;
	/**
	 * ...
	 * @author Scott 
	 */
	public class Ball extends Sprite
	{
		private var _shadowData:Sprite;		//影子
		private var _shadow:Matrix;
		private var _ballData:BitmapData;		//
		private var _lightData:Sprite;		//灯光
		private var _light:Matrix;
		private var _data:Bitmap;			//已经映射好的材质
		private var _dataBuf:BitmapData;		//原材质
		private var _mass:Sprite;
		
		//一堆经常临时变量
		private var i:int;
		private var j:int;
		private var d1:Number;
		private var d2:Number;
		private var angle:Number;
		private var ix:Number;
		private var iy:Number;
		
		//运动相关参数
		private var _angx:Number = 0;		//
		private var _angy:Number = 0;		//
		private var _angz:Number = 0;		//
		private var _px:Number = 0;
		private var _py:Number = 0;
		
		private var _vpx:Number = 0;// 8 - 20 * Math.random();
		private var _vpy:Number = 0;// 20 - 10 * Math.random();
		
		private var _vax:Number = 0;
		private var _vay:Number = 0;
		private var _vaz:Number = 0;
		
		public var num:int = 0;
		public var isOn:Boolean = true;
		private var r:Number = 128 / Math.PI;
		private var rl:Number=25;
		private var _scal:Number;
		
		public function Ball(num:int,data:BitmapData=null) 
		{
			super();
			if(data!=null) _dataBuf = data;
			this.num = num;
			_scal = 128/(12.5*Math.PI);
			init();
		}
		
		private function init():void 
		{
			//初始化阴影
			_shadowData = new Sprite();
			_shadow = new Matrix();
			_shadow.createBox(.025, .025, 0, 0, 0);

			//初始化灯光
			_lightData = new Sprite();
			_light = new Matrix();
			_light.createBox(0.019, 0.019, 0, 0, 0);
			//_lightData.blendMode = BlendMode.OVERLAY;
			
			//初始化球面
			_ballData = new BitmapData(r*2+2, r*2+2, true, 0x0);
			_data = new Bitmap(_ballData);
			_data.x = _data.y = -rl/2-.2;
			_data.blendMode = BlendMode.OVERLAY;
			
			//渲染
			reDraw();
			if (num == 0) initWhile();
			ranAngle();
			
			//添加到舞台
			this.addChild(_lightData);
			this.addChild(_data);
			this.addChild(_shadowData);
		}
		
		public function run(e:Event):void 
		{
			_angx += 1;
			reDraw();
		}
		
		public function move():void 
		{
			if (_vpx < Config.ignore && -vpx < Config.ignore && -_vpy < Config.ignore && _vpy < Config.ignore)
			{
				return;
			}
			
			_px += _vpx;
			_py += _vpy;
			_angx += _vax;
			_angy += _vay;
			_angz += _vaz;
			_vpx *= Config.bd_MoveXDec;
			_vpy *= Config.bd_MoveYDec;
			_vax *= Config.bb_CirXDec;
			_vay *= Config.bd_CirYDec;
			_vaz *= Config.bd_CirZDec;
			
			//_vpx = _vpx<.5&&_vpx>-.5?0:_vpx;
			//_vpy = _vpy<.5&&_vpy>-.5?0:_vpy;
			
			//var m:Number = _vax < 0? -_vax:_vax;
			//var n:Number = _vpx < 0? -_vpx:_vpx;
			//_vax = m < n / 1000?_vpx / 10000:_vax;
			//m = _vay < 0? -_vay:_vay;
			//n = _vpy < 0? -_vpy:_vpy;
			//_angy = m < n / 1000?_vpy / 10000:_vay;
			
			//_angx = (_angx < 0? -_angx:_angx) < (_vpx < 0? -_vpx:_vpx) / 5?_vpx / 5:_angx;
			//_angy = (_angy < 0? -_angy:_angy) < (_vpy < 0? -_vpy:_vpy) / 5?_vpy / 5:_angy;
			//_angz = _vaz < .5?0:_vaz;
			
			//_vax= _vpx *2;
			//_vax= _vpy *2;
			
			//_angx = int((_angx % 124) * 10) / 10;
			//_angy = int((_angy % 124) * 10) / 10;
			//_angz = int((_angz % 124) * 10) / 10;
			while (_angx > 124) _angx -= 124;
			while (_angx < 0) _angx += 124;
			while (_angy > 124) _angy -= 124;
			while (_angy < 0) _angy += 124;
			//_angx = _angx < 0?_angx + 124:_angx;
			//_angx = _angx >124?_angx - 124:_angx;
			//_angy = _angy < 0?_angy + 124:_angy;
			//_angy = _angy > 0?_angy - 124:_angy;
			
			
			//trace(_angx + "---" + _angy);
			reDraw();
		}
		
		public function ranAngle():void
		{
			_angx = 124 * Math.random();
			_angy = 124 * Math.random();	
		}
		
		public function reSet():void 
		{
			_vpx = 0;
			_vpy = 0;
			
			_vax = 0;
			_vay = 0;
			_vaz = 0;
		}   
		
		public function isMove():Boolean 
		{
			if (Math.abs(_vpx) > Config.ignore||Math.abs(_vpy) > Config.ignore)
			{
				return true;
			}
			return false;
		}
		
		public function reDraw():void 
		{
			this.x = _px;
			this.y = _py;
			
			
			_light.tx = (Config.deskCenter.x-_px) / 50;
			_light.ty = (Config.deskCenter.y - _py) / 50;
			
			_shadow.tx=(_px-Config.deskCenter.x) / 100;
			_shadow.ty = (_py - Config.deskCenter.y) / 100;
			
			changeLight();
			if(num!=0) drawBall();
		}
		
		private function initWhile():void 
		{
			if (num == 0) 
			{
				var sp:Sprite = new Sprite();
				sp.graphics.beginFill(0xEFEFEF, 1)
				sp.graphics.drawCircle(13, 13, 12.5);
				sp.graphics.endFill();
				_ballData.draw(sp);
			}
		}
		
		private function changeLight():void
		{
			//绘制灯光和阴影
			_lightData.graphics.clear();
			_lightData.graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0x000000], [.8, .8], [0, 255], _light);
			_lightData.graphics.drawCircle(0, 0, 12.5);
			_lightData.graphics.endFill();
			
			_shadowData.graphics.clear();
			_shadowData.graphics.beginGradientFill(GradientType.RADIAL, [0, 0], [.75, 0], [92, 195], _shadow);
			_shadowData.graphics.drawCircle(0, 0, 17.5);
			_shadowData.graphics.drawCircle(0, 0, 12.5);
			_shadowData.graphics.endFill();
		}
		
		private function drawBall():void 
		{
			//绘球
			if (num == 0) return;
			_ballData.lock();
			for (i = 0; i < 25; i++) {
				for (j = 0; j < 25; j++) {
					ix=i*_scal;
					iy=j*_scal;
					d1 = Math.sqrt((ix - r) * (ix - r) + (iy - r) * (iy - r));
					if (d1>=r) {
						continue;
					}
					d2=Math.asin(d1/r)*r;
					angle=Math.atan2(ix-r,iy-r);
					ix=Math.cos(angle)*d2+_angx+62;
					iy=Math.sin(angle)*d2+_angy+62;
					_ballData.setPixel32(j, i, _dataBuf.getPixel32(ix, iy));
				}
			}
			_ballData.unlock();
		}
		
		
		private function setAngle(ax:Number,ay:Number):void 
		{
			
		}
		
		public function setLight():void 
		{
			reDraw();
		}
		
		public function setPos(px:Number,py:Number):void 
		{
			this._px = px;
			this._py = py;
			reDraw();
		}
		
		public function addX():void 
		{
			//if (_vpx != 0)
			//{
				//this.x += _vpx;
				//_angx -= _vpx/2;
				//_angx = _angx > 31?0:_angx;
				//_angx = _angx < 0?31:_angx;
				//
				//setAngle(_angx,_angy);
				//setLight();
			//}
		}
		
		public function addY(py:Number):void 
		{
			
		}
		
		public function set px(value:Number):void 
		{
			_px = value;
		}
		public function set vpx(value:Number):void 
		{
			_vpx = value;
		}
		public function set py(value:Number):void 
		{
			_py = value;
		}
		public function set vpy(value:Number):void 
		{
			_vpy = value;
		}
		
		public function get px():Number { return _px; }
		public function get vpx():Number
		{ 
			return _vpx; 
		}
		public function get py():Number { return _py; }
		public function get vpy():Number { return _vpy; }
		
		public function set vax(value:Number):void 
		{
			_vax = value;
		}
		public function get vax():Number { return _vax; }
		
		public function set vay(value:Number):void 
		{
			_vay = value;
		}
		public function get vay():Number { return _vay; }
	}

}